package com.zxl.always.on.display.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.bean.PaletteItem;
import com.zxl.always.on.display.ui.widget.NonePaddingTextView;

public class GraphicUtils {
    private static final String TAG = GraphicUtils.class.getSimpleName();

    public static Bitmap drawableToBitmap(Drawable paramDrawable, int paramInt1, int paramInt2) {
        if (paramDrawable == null) {
            return null;
        }
        Bitmap localBitmap = Bitmap.createBitmap(paramInt1, paramInt2, Bitmap.Config.ARGB_8888);
        paramDrawable.draw(new Canvas(localBitmap));
        return localBitmap;
    }

    public static int getFractionColor(int paramInt1, int paramInt2, float paramFloat) {
        int i = Color.alpha(paramInt1);
        int j = Color.red(paramInt1);
        int k = Color.green(paramInt1);
        int m = Color.blue(paramInt1);
        int n = Color.alpha(paramInt2) - i;
        int i1 = Color.red(paramInt2) - j;
        int i2 = Color.green(paramInt2) - k;
        int i3 = Color.blue(paramInt2) - m;
        return Color.argb(i + (int) (paramFloat * n), j + (int) (paramFloat * i1), k + (int) (paramFloat * i2), m + (int) (paramFloat * i3));
    }

    public static BitmapDrawable getGlowDrawable(Context paramContext, Drawable paramDrawable, int paramInt1, int paramInt2, BlurMaskFilter.Blur paramBlur, int paramInt3, int paramInt4, int paramInt5, boolean paramBoolean) {
        if (paramDrawable == null) {
            Log.e(TAG, "getGlowDrawable : null drawable");
            return null;
        }
        if (paramBlur == null) {
            paramBlur = BlurMaskFilter.Blur.OUTER;
        }
        BlurMaskFilter localBlurMaskFilter = new BlurMaskFilter(paramInt3, paramBlur);
        Paint localPaint = new Paint(1);
        localPaint.setDither(true);
        localPaint.setMaskFilter(localBlurMaskFilter);
        localPaint.setColor(paramInt4);
        Bitmap localBitmap1 = drawableToBitmap(paramDrawable, paramInt1, paramInt2);
        Bitmap localBitmap2 = localBitmap1.extractAlpha();
        localBitmap1.recycle();
        Bitmap localBitmap3 = Bitmap.createBitmap(paramInt1 + paramInt5 * 2, paramInt2 + paramInt5 * 2, Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap3);
        localCanvas.drawBitmap(localBitmap2, paramInt5, paramInt5, localPaint);
        if (!paramBoolean) {
            paramDrawable.setBounds(paramInt5, paramInt5, paramInt5 + paramInt1, paramInt5 + paramInt2);
            paramDrawable.draw(localCanvas);
        }
        localBitmap2.recycle();
        BitmapDrawable localBitmapDrawable = new BitmapDrawable(paramContext.getResources(), localBitmap3);
        localBitmapDrawable.setBounds(0, 0, localBitmap3.getWidth(), localBitmap3.getHeight());
        return localBitmapDrawable;
    }

    public static BitmapDrawable getLinearGradientDrawable(Context context, Drawable drawable, int width, int height, float rotate, LinearGradient linearGradient) {
        if ((context == null) || (drawable == null) || (linearGradient == null)) {
            return null;
        }
        Paint paint = new Paint();
        paint.setColor(-16777216);
        paint.setDither(false);
        paint.setStrokeWidth(10.0F);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        paint.setShader(linearGradient);
        Matrix matrix = new Matrix();
        linearGradient.getLocalMatrix(matrix);
        matrix.postRotate(rotate, width / 2.0F, height / 2.0F);
        linearGradient.setLocalMatrix(matrix);
        drawable.setBounds(0, 0, width, height);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        canvas.drawRect(0.0F, 0.0F, width, height, paint);
        return new BitmapDrawable(context.getResources(), bitmap);
    }

    public static BitmapDrawable getRadialGradientDrawable(Context paramContext, Drawable paramDrawable, int paramInt1, int paramInt2, RadialGradient paramRadialGradient) {
        if ((paramContext == null) || (paramDrawable == null) || (paramRadialGradient == null)) {
            return null;
        }
        Paint localPaint = new Paint();
        localPaint.setColor(-16777216);
        localPaint.setDither(false);
        localPaint.setStrokeWidth(10.0F);
        localPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        localPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        localPaint.setShader(paramRadialGradient);
        paramDrawable.setBounds(0, 0, paramInt1, paramInt2);
        Bitmap localBitmap = Bitmap.createBitmap(paramInt1, paramInt2, Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(localBitmap);
        paramDrawable.draw(localCanvas);
        localCanvas.drawCircle(paramInt1 / 2.0F, paramInt2 / 2.0F, paramInt1 / 2.0F, localPaint);
        return new BitmapDrawable(paramContext.getResources(), localBitmap);
    }

    public static Rect getSingleLineTextBound(TextView textView, CharSequence charSequence) {
        Rect rect = new Rect();
        if ((textView != null) && (!TextUtils.isEmpty(charSequence))) {
            String str = charSequence.toString();
            textView.getPaint().getTextBounds(str, 0, str.length(), rect);
        }
        return rect;
    }

    public static Drawable getSweepGradientDrawable(Context context, Drawable drawable, int width, int height, float rotate, SweepGradient sweepGradient) {
        if ((context == null) || (drawable == null) || (sweepGradient == null)) {
            return null;
        }
        Paint localPaint = new Paint();
        localPaint.setColor(-16777216);
        localPaint.setDither(true);
        localPaint.setStrokeWidth(10.0F);
        localPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        localPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        localPaint.setShader(sweepGradient);
        Matrix localMatrix = new Matrix();
        sweepGradient.getLocalMatrix(localMatrix);
        localMatrix.postRotate(rotate, width / 2.0F, height / 2.0F);
        sweepGradient.setLocalMatrix(localMatrix);
        drawable.setBounds(0, 0, width, height);
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
        drawable.draw(new Canvas(bitmap));
        Bitmap localBitmap2 = Bitmap.createScaledBitmap(bitmap, width, height, true);
        new Canvas(localBitmap2).drawCircle(width / 2.0F, height / 2.0F, width / 2.0F, localPaint);
        return new BitmapDrawable(context.getResources(), localBitmap2);
    }

    public static void setLinearGradientTextColor(TextView textView, PaletteItem paletteItem) {
        if (paletteItem.getType() == PaletteItem.Type.SOLID) {
            textView.setTextColor(paletteItem.getColor());
            textView.getPaint().setShader(null);
        } else {
            int measuredHeight = textView.getMeasuredHeight();
            int measuredWidth = textView.getMeasuredWidth();
            LinearGradient linearGradient = new LinearGradient(0, 0, measuredWidth, measuredHeight, paletteItem.getColors(), paletteItem.getPositions(), Shader.TileMode.CLAMP);
            Matrix matrix = new Matrix();
            linearGradient.getLocalMatrix(matrix);
            matrix.postRotate(-30, measuredWidth / 2, measuredHeight / 2);
            linearGradient.setLocalMatrix(matrix);
            textView.getPaint().setShader(linearGradient);
            textView.getPaint().setColor(-1);
        }
        textView.invalidate();
    }
}
